﻿using ET_DbCommon.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET_DbCommon.DAL
{
    public static class ExamDAL
    {

        /// <summary>
        /// 创建试卷
        /// </summary>
        /// <param name="MyPaperId"></param>
        public static Task InsertToExam(List<ExamModel> examModels, int MyPaperId, UserListVo userListVo)
        {
            try
            {
                using (var conn = MySqlHelper.GetConnection())
                {
                    var MyPaperModel = PaperDAL.GetMyPaperModel(MyPaperId);
                    if (MyPaperModel != null)
                    {
                        ExamModel MyExamModel = new ExamModel();
                        MyExamModel.Describes = MyPaperModel.Describes;
                        MyExamModel.TesquestiontList = MyPaperModel.TesquestiontList;
                        MyExamModel.TotalScore = MyPaperModel.TotalScore;
                        MyExamModel.CreateTime = DateTime.Now;
                        MyExamModel.UpdateTime = DateTime.Now;
                        MyExamModel.PaperId = MyPaperId;
                        var lastId = MySQLExt.InsertIdentityIndex<ExamModel>(conn, MyExamModel);
                        MyExamModel.Id = lastId;
                        if (examModels.All(model => !model.Equals(MyExamModel)))
                        {
                            examModels.Add(MyExamModel);
                            userListVo.MyExam = MyExamModel;
                        }
                    }
                }
                return Task.CompletedTask;
            }
            catch(Exception ex)
            {
                throw (ex);
            }
        }
    }
}
